﻿using System;
using System.Web.Mvc;
using mvcForum.AddOns.Akismet;
using mvcForum.AddOns.ViewModels;
using mvcForum.Core.Interfaces.AddOns;
using mvcForum.Web.Controllers;
using mvcForum.Web.Helpers;
using mvcForum.Web.Interfaces;
using mvcForum.Core.Interfaces.Data;

namespace mvcForum.AddOns.Controllers {

	public class AkismetConfigurationController : ForumAdminBaseController, IAntiSpamConfigurationController {
		private readonly AkismetConfiguration config;

		public AkismetConfigurationController(IWebUserProvider userProvider, IContext context, IAddOnConfiguration<AkismetAddOn> config)
			: base(userProvider, context) {

			this.config = (AkismetConfiguration)config;
		}

		public ViewResult Index() {
			return View(new AkismetViewModel { Key = this.config.Key, Enabled = this.config.Enabled, SpamScore = this.config.SpamScore, MarkAsSpamOnHit = this.config.MarkAsSpamOnHit, RunAsynchronously = this.config.RunAsynchronously, Delay = this.config.Delay });
		}

		[HttpPost]
		public ViewResult Index(AkismetViewModel model) {
			if (ModelState.IsValid) {
				this.config.Enabled = model.Enabled;
				this.config.Key = model.Key;
				if (model.SpamScore.HasValue) {
					this.config.SpamScore = model.SpamScore.Value;
				}
				this.config.MarkAsSpamOnHit = model.MarkAsSpamOnHit;
				this.config.RunAsynchronously = model.RunAsynchronously;
				this.config.Delay = model.Delay;

				this.Context.SaveChanges();

				TempData["Saved"] = true;
			}

			return View(model);
		}

		public String Name {
			get {
				return ForumHelper.GetString("Name", null, "mvcForum.AddOns.Akismet");
			}
		}

		public String Description {
			get {
				return ForumHelper.GetString("Description", null, "mvcForum.AddOns.Akismet");
			}
		}
	}
}